package com.zkr.kfsz.service.impl;

import com.zkr.kfsz.mapper.KFSZMapper;
import com.zkr.kfsz.model.*;
import com.zkr.kfsz.service.KFSZService;
import com.zkr.kufang.model.KuFangVo;
import com.zkr.subclass.enummodel.StatusEnum;
import com.zkr.thresho.model.SubClassVo;
import com.zkr.util.AllRecords;
import com.zkr.util.ResultBuilderVo;
import com.zkr.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by lenovo on 2021/1/14.
 */
@Service
public class KFSZServiceImpl implements KFSZService{

    @Autowired
    private KFSZMapper kfszMapper;
    @Override
    public ResultVo queryPage(KFSZSelectVo kfszSelectVo) {
        int count = kfszMapper.queryCount(kfszSelectVo);
        List<KFSZVo> kfszVoList = kfszMapper.queryPage(kfszSelectVo);
        for (KFSZVo kfszVo:kfszVoList){
            Integer status =kfszVo.getStatus()==true?1:0;
            for (StatusEnum c:StatusEnum.values()){
                if (c.getValue().equals(status)){
                    kfszVo.setStatusName(c.getName());
                }
            }
        }

        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(kfszSelectVo.getPageIndex());
        allRecords.setDataList(kfszVoList);
        allRecords.setPageSize(kfszSelectVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);
        return ResultBuilderVo.success(allRecords);


    }
    @Transactional
    @Override
    public ResultVo updateStatus(@Valid KFSZUpdateStatus kfszUpdateStatus) {

        KFSZVo kfszVo = new KFSZVo();
        BeanUtils.copyProperties(kfszUpdateStatus,kfszVo);
        kfszVo.setUpdateTime(new Date());
        kfszVo.setUpdateName("李四");
        kfszMapper.updateStatus(kfszVo);
        return ResultBuilderVo.success();
    }

    @Override
    @Transactional
    public ResultVo add(@Valid KFSZAddVo kfszAddVo) {
        KFSZVo  kfszCode = kfszMapper.queryCode(kfszAddVo.getCode());
        if (kfszCode !=null){
            return ResultBuilderVo.error("库房编码已经存在");
        }
        KFSZVo kfszName = kfszMapper.queryName(kfszAddVo.getName());
        if(kfszName !=null){
            return ResultBuilderVo.error("库房标识名称已经存在");
        }
        KFSZVo kfszVo = new KFSZVo();
        kfszVo.setCreateName("张三");
        kfszVo.setCreateTime(new Date());
        kfszVo.setUpdateName("张三");
        kfszVo.setUpdateTime(new Date());

        //把左边的数据赋值给右边
        BeanUtils.copyProperties(kfszAddVo,kfszVo);
        //入库
        kfszMapper.insertkfsz(kfszVo);
        return ResultBuilderVo.success();


    }

    @Override
    public ResultVo queryKF() {
        List<KFVo> kfVoList = kfszMapper.querykf();
        return ResultBuilderVo.success(kfVoList);
    }

    @Override
    @Transactional
    public ResultVo update(@Valid KFSZUpdateVo kfszUpdateVo) {
        KFSZVo kfszName = kfszMapper.queryName(kfszUpdateVo.getName());
        if (kfszName != null){
            if (!(kfszName.getWarehouseId().equals(kfszUpdateVo.getWarehouseId()))){
                return ResultBuilderVo.error("库房名称已经存在");
            }

        }

        KFSZVo kfszVo = new KFSZVo();
        BeanUtils.copyProperties(kfszUpdateVo,kfszVo);
        kfszVo.setUpdateTime(new Date());
        kfszVo.setUpdateName("李四");
        kfszMapper.update(kfszVo);

        return ResultBuilderVo.success();

    }

}
